﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using RoomPriceEntity;

namespace ProDataUpdater
{
    public enum MemberGradeEnum
    {
        NONE = 1 << 0,
        EDM = 1 << 1,
        NORMAL = 1 << 2,
        GOLD = 1 << 3,
        PLATINUM = 1 << 4,
        DIAMOND = 1 << 5,
        WEIXIN = 1 << 6,
        NOT_DEFINE = 1 << 7,
    }
    public class Program
    {
        public static void Main(string[] args)
        {
            //MemberGradeEnum result =
            //                    MemberGradeEnum.NORMAL
            //                    | MemberGradeEnum.EDM
            //                    | MemberGradeEnum.GOLD
            //                    | MemberGradeEnum.PLATINUM
            //                    | MemberGradeEnum.DIAMOND
            //                    | MemberGradeEnum.WEIXIN
            //                    | MemberGradeEnum.NOT_DEFINE;
            //int memberGrade = (int)result;

            //Console.WriteLine(memberGrade);
            //Console.WriteLine();

            //Console.WriteLine(1 << 0);
            //Console.WriteLine(1 << 1);
            //Console.WriteLine(1 << 2);
            //Console.WriteLine(1 << 3);
            //Console.WriteLine(1 << 4);
            //Console.WriteLine(1 << 5);
            //Console.ReadLine();

            update(@"

");
            //Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            //Console.ReadLine();
        }

        public static int BufferLength = 1000;

        public static List<string> XmlSplitterToList(string xml)
        {
            List<string> result = new List<string>();
            if (string.IsNullOrEmpty(xml)) return result;

            while (xml.Length != 0)
            {
                int subIndex = BufferLength;
                if (xml.Length <= BufferLength)
                {
                    subIndex = xml.Length;
                }
                string temp = xml.Substring(0, subIndex);
                //Console.WriteLine(temp);
                result.Add(temp);
                xml = xml.Substring(subIndex);
                //Console.WriteLine("remain:" + xml);
            }

            return result;
        }

        public static void update(string sp)
        {
            Database db = null;
            //db = DatabaseFactory.CreateDatabase("HotelPubDB_INSERT");
            db = DatabaseFactory.CreateDatabase("ProductDB_SELECT_pro");
            DbCommand dbCommand = db.GetSqlStringCommand(sp);
            dbCommand.CommandTimeout = 0;
            db.ExecuteNonQuery(dbCommand);
        }

        public static void SelectResult()
        {

        }
    }

    public static class InterfaceDB
    {
        public static int GetRoomByHotelCode(string HotelCode)
        {
            int result = 0;
            Database db = null;
            db = DatabaseFactory.CreateDatabase("ProductDB_SELECT_pro");
            DbCommand dbCommand = db.GetSqlStringCommand(@"Select Room From HotelInterfaceDB..HotelGroup_InterfaceRoomType(nolock) a
inner join HotelInterfaceDb..HotelGroup_InterfaceHotel(nolock) b on a.hotel = b.hotel
 Where HotelGroupHotelCode = @HotelCode and b.GroupId = 32");
            db.AddInParameter(dbCommand, "@HotelCode", System.Data.DbType.String, HotelCode);
            dbCommand.CommandTimeout = 0;
            using (var idr = db.ExecuteReader(dbCommand))
            {
                while (idr.Read())
                {
                    result = Convert.ToInt32(idr["Room"]);
                }
            }
            return result;
        }

        public static List<long> GetStrategyID(string HotelCode)
        {
            List<long> result = new List<long>();
            Database db = null;
            db = DatabaseFactory.CreateDatabase("ProductDB_SELECT_pro");
            DbCommand dbCommand = db.GetSqlStringCommand(@"Select PriceCacheStrategyId From HotelInterfaceDB..Htl_If_PriceCacheStrategy(nolcok) where HotelCode = @HotelCode and GroupId=32");
            db.AddInParameter(dbCommand, "@HotelCode", System.Data.DbType.String, HotelCode);
            dbCommand.CommandTimeout = 0;
            using (var idr = db.ExecuteReader(dbCommand))
            {
                while (idr.Read())
                {
                    result.Add(Convert.ToInt64(idr["PriceCacheStrategyId"]));
                }
            }
            return result;
        }

        public static List<PriceCacheStrategyEntity> GetPriceStrategy(string hotelCode)
        {
            List<PriceCacheStrategyEntity> result = new List<PriceCacheStrategyEntity>();
            Database db = null;
            db = DatabaseFactory.CreateDatabase("ProductDB_SELECT_pro");
            DbCommand dbCommand = db.GetSqlStringCommand(@"Select top 5 * From HotelInterfaceDB..Htl_If_PriceCacheStrategy(nolock) where HotelCode = @HotelCode and GroupId=33 and IsEnable ='T'");
            db.AddInParameter(dbCommand, "@HotelCode", System.Data.DbType.String, hotelCode);
            dbCommand.CommandTimeout = 0;
            using (var idr = db.ExecuteReader(dbCommand))
            {
                while (idr.Read())
                {
                    PriceCacheStrategyEntity entity = DBHelper.ConvertToEntity<PriceCacheStrategyEntity>(idr);
                    result.Add(entity);
                }
            }
            return result;
        }
    }
}
